<?php
define('CAT_PATH', 'code/code.json');
define('CDN', 'http://tk.wokill.com/');
$cate = json_decode(file_get_contents(CAT_PATH), true);
$default = isset($_GET['g']) && !empty($_GET['g']) ? $_GET['g'] : '704d5c7355ae1b3331574ed19d3fbe59';
$goods_file = 'goods/' . $default . '.json';
$goods = json_decode('['.trim(file_get_contents($goods_file),',').']', true);
$page = isset($_GET['p']) && !empty($_GET['p']) ? $_GET['p'] : 1;
$list_num = isset($_GET['list']) && !empty($_GET['list']) ? $_GET['list'] : 54;
$total_page = ceil(count($goods) / $list_num);
?>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <title>推广商品采集页</title>
</head>
<style>
    .showGoods{border: 1px solid salmon; padding: 5px;height: 450px; margin-bottom: 15px;}
    @media screen and (max-width: 1200px){
        .showGoods{height: 27em;}
    }
</style>
<body>
<nav class="navbar navbar-default col-xs-12">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a class="navbar-brand" href="#">栏目分级</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <?php $i = 0;
                foreach ($cate as $key => $val): ?>
                    <li class="<?= $key == $default ? 'active' : '' ?>"><a
                            href="?g=<?= $key ?>"><?= $val ?> </a></li>
                <?php endforeach; ?>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-xs-12" style="padding-left: 35px; margin-bottom: 20px; border-bottom: 2px #cccccc double"><h4>
                商品展示区</h4></div>

        <div class="col-xs-12">
            <?php for ($i = ($page - 1) * $list_num, $len = $page * $list_num; $i < $len; $i++): if (!isset($goods[$i])) {
                break;
            } ?>
                <div class="col-xs-3 col-md-2">
                    <div class="col-xs-12 center-block showGoods" style="">
                        <div class="c_top ">
                            <a href="<?= $goods[$i]['tk_url'] ?>" target="_blank">
                                <img src="<?= $goods[$i]['img'] ?>"
                                     style="width: 100%; height: 100%; max-height: 273px;"
                                     class="img-rounded">
                            </a>
                        </div>
                        <div class="c_bottom">
                            <div class="c_content">
                                <h6><a href="<?= $goods[$i]['tk_url'] ?>"
                                       target="_blank"><strong><?= $goods[$i]['name'] ?></strong></a></h6>
                                <p>价格：<span class="text-danger"><?= $goods[$i]['price'] ?></span></p>
                                <p>优惠券：<a href="<?= $goods[$i]['cn_tk_url'] ?>"
                                          target="_blank"><span><?= $goods[$i]['youMain'] ?></span></a></p>
                                <p>佣金比列：<span class="text-danger"><?= $goods[$i]['yongBi'] ?>%</span></p>
                                <button type="button" class="btn text-center bg-success goods_copy"
                                        style="width: 100%; padding: 5px;"
                                        data-copy-img="<?=CDN.$goods[$i]['id'].'.jpg'?>"
                                        data-img="<?=CDN.$goods[$i]['id'].'.jpg'?>">
                                    复制文案
                                </button>
                                <div class="copy_content hide">
                                    <p><?= $goods[$i]['name'] ?></p>
                                    <p>单价：<?= $goods[$i]['price'] ?></p>
                                    <p><?= $goods[$i]['youMain'] ?>优惠券:&nbsp;&nbsp;<?= $goods[$i]['cn_tk_url'] ?></p>
                                    <p>下单链接:&nbsp;&nbsp; <?= $goods[$i]['tk_url'] ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endfor; ?>
        </div>
        <div class="col-xs-12 center-block text-center" style="margin-top: 40px;">
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <?php if ($page <= 1): ?>
                        <li class="<?= $page <= 1 ? 'disabled' : '' ?>">
                            <a aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    <?php else: ?>
                        <li>
                            <a href="?g=<?= $default . '&p=' . ($page - 1) ?>" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    <?php endif ?>

                    <?php for ($p = 1; $p <= $total_page; $p++): ?>
                        <li class="<?= $page == $p ? 'active' : '' ?>"><a
                                href="?g=<?= $default . '&p=' . $p ?>"><?= $p ?></a></li>
                    <?php endfor; ?>

                    <?php if ($page + 1 > $total_page): ?>
                        <li class="<?= $page + 1 > $total_page ? 'disabled' : '' ?>">
                            <a aria-label="Next">
                            <span aria-hidden="true">&raquo;
                            </a>
                        </li>
                    <?php else: ?>
                        <li>
                            <a href="?g=<?= $default . '&p=' . ($page + 1) ?>" aria-label="Next">
                            <span aria-hidden="true">&raquo;
                            </a>
                        </li>
                    <?php endif ?>
                </ul>
            </nav>
        </div>
    </div>
</div>


<script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="./js/clipboard.js"></script>
<script src="./js/layer.js"></script>
<script>
    $(function () {
        clipBoard('.goods_copy');
    });

    function clipBoard(dom) {
        $(dom).on("click", function (event) {
            var event = event || window.event;
            event.preventDefault();
            //获取复制的图文信息
            var html, txt, imgSrc;
            txt = $.trim($(this).parents(".c_content").find(".copy_content").text());
            txt = txt.replace(/\n/ig, "<br/>");
            imgSrc = $(this).attr("data-copy-img");
            html = "<div id='copyInput' class='copyInput'><img src='" + imgSrc + "' style='width: 280px;' width='280'  ><br/>" + txt + "</div>";
            var tbCopyImgUrl = $(this).data("img");
            try {
                window.external.Copy(tbCopyImgUrl, txt);
            } catch (error) {

            }
            $("#copyInput").remove();
            $("body").append(html);
            //复制方法
            var indexClip = new Clipboard('.goods_copy', {
                target: function () {
                    return document.getElementById('copyInput');
                }
            });
            indexClip.on('success', function (e) {
                layer.msg('已复制');
                $("#copyInput").remove();
                e.clearSelection();
            });
            indexClip.on('error', function (e) {
                layer.msg('您的浏览器不支持一键复制，请升级浏览器或更换浏览器');
                $("#copyInput").remove();
                e.clearSelection();
            });

        });
    }

</script>
</body>
</html>